CFLAGS =  -c -Wall -O0 -g3 
CC = gcc 
AR = ar
LD = ld


ifeq ($(B),embed) 
	FL = -DEMBED
	BUILD = bootload linker compile link copyprog
	BUILDDIR = embed_build
else
	BUILD = win
	BUILDDIR = build
endif

all: dir $(BUILD)

dir:
	mkdir -p $(BUILDDIR)

bootload: embed\bootload.bin

linker: link.cmd

compile: $(BUILDDIR)\main.o $(BUILDDIR)\embed.o $(BUILDDIR)\codepage.o temp.o $(BUILDDIR)\video-graphics.o $(BUILDDIR)\video-text.o $(BUILDDIR)\menu.o $(BUILDDIR)\game.o $(BUILDDIR)\ints.o kbc_isr.o $(BUILDDIR)\player.o $(BUILDDIR)\rtc.o $(BUILDDIR)\timer.o $(BUILDDIR)\music.o $(BUILDDIR)\serial.o $(BUILDDIR)\sprite.o 
 
link: embedded.bin

copyprog:
	-copy embedded.bin a:

#----------------------------

embed\bootload.bin: embed\copyboot.exe embed\bootload.asm
	cd embed
	nasm -f bin -o bootload.bin bootload.asm
	-copyboot a:
	cd ..

embed\ldscript.exe: embed\ldscript.c
	cd embed
	$(CC) ldscript.c -o ldscript.exe
	cd ..
	
embed\copyboot.exe: embed\copyboot.c
	cd embed
	$(CC) copyboot.c -o copyboot.exe
	cd ..
	
link.cmd: embed\ldscript.exe
	embed\ldscript > link.cmd

embedded.bin: compile
	$(LD) $(BUILDDIR)\main.o $(BUILDDIR)\embed.o $(BUILDDIR)\codepage.o temp.o $(BUILDDIR)\player.o $(BUILDDIR)\menu.o $(BUILDDIR)\game.o $(BUILDDIR)\rtc.o $(BUILDDIR)\timer.o $(BUILDDIR)\music.o $(BUILDDIR)\serial.o libc.a libepc.a -Tlink.cmd -u start

#----------------------------

win: $(BUILDDIR)\main.o lib
	$(CC) $(BUILDDIR)\main.o liblcom.a -o hangman.exe
	
	
lib : temp.o $(BUILDDIR)\codepage.o $(BUILDDIR)\video-graphics.o $(BUILDDIR)\video-text.o $(BUILDDIR)\queue.o $(BUILDDIR)\menu.o $(BUILDDIR)\game.o $(BUILDDIR)\ints.o kbc_isr.o $(BUILDDIR)\player.o $(BUILDDIR)\serial.o $(BUILDDIR)\timer.o $(BUILDDIR)\music.o $(BUILDDIR)\rtc.o $(BUILDDIR)\sprite.o  $(BUILDDIR)\kbc.o mouse_isr.o  $(BUILDDIR)\mouse.o
	$(AR) -cr liblcom.a  temp.o $(BUILDDIR)\codepage.o $(BUILDDIR)\video-text.o $(BUILDDIR)\video-graphics.o $(BUILDDIR)\queue.o $(BUILDDIR)\menu.o $(BUILDDIR)\game.o $(BUILDDIR)\ints.o kbc_isr.o $(BUILDDIR)\player.o $(BUILDDIR)\serial.o $(BUILDDIR)\timer.o $(BUILDDIR)\music.o $(BUILDDIR)\rtc.o $(BUILDDIR)\sprite.o  $(BUILDDIR)\kbc.o mouse_isr.o  $(BUILDDIR)\mouse.o kbc_init.o

$(BUILDDIR)\main.o: main.c compat.h
	$(CC) $(FL) $(CFLAGS) main.c -o $(BUILDDIR)\main.o

$(BUILDDIR)\embed.o: embed.c
	$(CC) $(FL) $(CFLAGS) embed.c -o $(BUILDDIR)\embed.o
	
$(BUILDDIR)\codepage.o: codepage.c
	$(CC) $(FL) $(CFLAGS) codepage.c -o $(BUILDDIR)\codepage.o

temp.o: temp.asm
	nasm -t -f coff temp.asm -o temp.o
	
$(BUILDDIR)\video-text.o: video-text.c video-text.h
	$(CC) $(FL) $(CFLAGS) video-text.c -o $(BUILDDIR)\video-text.o
	
$(BUILDDIR)\video-graphics.o: video-graphics.c video-graphics.h
	$(CC) $(FL) $(CFLAGS) video-graphics.c -o $(BUILDDIR)\video-graphics.o
	
$(BUILDDIR)\menu.o: menu.c
	$(CC) $(FL) $(CFLAGS) menu.c -o $(BUILDDIR)\menu.o

$(BUILDDIR)\game.o: game.c
	$(CC) $(FL) $(CFLAGS) game.c -o $(BUILDDIR)\game.o

$(BUILDDIR)\ints.o: ints.c
	$(CC) $(FL) $(CFLAGS) ints.c -o $(BUILDDIR)\ints.o

$(BUILDDIR)\player.o: player.c
	$(CC) $(FL) $(CFLAGS) player.c -o $(BUILDDIR)\player.o

$(BUILDDIR)\queue.o: queue.c
	$(CC) $(FL) $(CFLAGS) queue.c -o $(BUILDDIR)\queue.o
	
$(BUILDDIR)\serial.o: serial.c serial.h
	$(CC) $(FL) $(CFLAGS) serial.c -o $(BUILDDIR)\serial.o

$(BUILDDIR)\music.o: music.c music.h
	$(CC) $(FL) $(CFLAGS) music.c -o $(BUILDDIR)\music.o

$(BUILDDIR)\timer.o: timer.c timer.h
	$(CC) $(FL) $(CFLAGS) timer.c -o $(BUILDDIR)\timer.o
	
$(BUILDDIR)\rtc.o: rtc.c rtc.h
	$(CC) $(FL) $(CFLAGS) rtc.c -o $(BUILDDIR)\rtc.o

$(BUILDDIR)\kbc.o: kbc.c kbc.h
	$(CC) $(FL) $(CFLAGS) kbc.c -o $(BUILDDIR)\kbc.o
	
$(BUILDDIR)\sprite.o: sprite.c sprite.h
	$(CC) $(FL) $(CFLAGS) sprite.c -o $(BUILDDIR)\sprite.o

$(BUILDDIR)\mouse.o: mouse.c mouse.h
	$(CC) $(FL) $(CFLAGS) mouse.c -o $(BUILDDIR)\mouse.o
	
kbc_isr.o:  kbc_isr.asm 
	nasm -t -f coff kbc_isr.asm -o kbc_isr.o
	
mouse_isr.o:  mouse_isr.asm 
	nasm -t -f coff mouse_isr.asm -o mouse_isr.o
	
clean:
	-cp kbc_init.o back
	-rm *.o 
	-mv back kbc_init.o
	-rm -r build
	-rm -r embed_build
	-rm embedded.bin
	-rm embed\bootload.bin
	-rm link.cmd
	-rm embed\ldscript.exe
	-rm hangman.exe
	
